#include "sdk_common.h"
#include "sdk_model_light.h"
#include "sdk_proto.h"
#include <stdio.h>
#include <string.h>
#include "sdk.h"


int sdk_model_light_ctl(ENUM_LIGHT_CTL_TYPE type,P_LIGHT_DATA data)
{
    char json[500]={0};
    char ctldata[100]={0};
    uint16_t addr = 0;
    uint16_t attrid= 0;
    if(data== NULL){
        sdk_printf("%s:null pointer\r\n",__FUNCTION__);
        return SDK_NULLPTR_ERR;
    }

    if(sdk_proto_context_get()==NULL){
        sdk_printf("%s:sdk context is null\r\n",__FUNCTION__);
        return SDK_UNAUTH_ERR;
    }

    memset(ctldata,0,sizeof(ctldata));
    

    switch(type)
    {
        case enum_light_ctl_cw:
            //"D1F9091607108064003600"
            sprintf(ctldata,"%02x%02x00%02x00",((data->cw.onoff & 0x01)<<7) |0x00,data->cw.lnss,data->cw.colortemp);
            addr = data->cw.addr;
            attrid = ATTRID1007HEX;
        break;

        case enum_light_ctl_color:
            //"D1F9091A07108100003164"
            sprintf(ctldata,"%02x%02x%02x%02x%02x",((data->color.onoff & 0x01)<<7) |0x01,data->color.h & 0xFF,(data->color.h>>8)&0xFF,data->color.s,data->color.b);
            addr = data->color.addr;
            attrid = ATTRID1007HEX;
        break;

        case enum_light_ctl_sence:
            //"D1F9092207108200000000"
            sprintf(ctldata,"%02x%02x%02x0000",((data->sence.onoff & 0x01)<<7) |0x02,data->sence.senceid & 0xFF,(data->sence.senceid >>8)& 0xFF);
            addr = data->sence.addr;
            attrid = ATTRID1007HEX;
        break;

        case enum_light_ctl_mutil_sw:
            sdk_printf("%s:unsupport option now\r\n",__FUNCTION__);
        break;

        case enum_light_ctl_rythem_cw:
            sdk_printf("%s:unsupport option now\r\n",__FUNCTION__);
        break;

        case enum_light_ctl_rythem_color:
            sdk_printf("%s:unsupport option now\r\n",__FUNCTION__);
        break;
        case enum_light_ctl_rgb_mode:
            //"D1F9094122100250"
            sprintf(ctldata,"%02x%02x",data->rgbmode.mode,data->rgbmode.speed);
            addr = data->rgbmode.addr;
            attrid = SDK_LIGHT_RGBMODE_ATTRID;
        break;

        default:
            sdk_printf("%s:unkown option\r\n",__FUNCTION__);
        break;
    }

    sprintf(json,"{\"call\":\"ai.argrace.gw.data.set\",\"ak\":\"%s\",\"gwid\":%u,\"ad\":\"%04x\",\"op\":\"%s\",\"vma\":\"%02x%02x\",\"data\":\"%s\",\"tid\":\"%02x\",\"mac\":\"%s\"}",\
        sdk_proto_context_get()->accesskey,sdk_proto_context_get()->gwid,addr,VMAD1,attrid&0xFF,(attrid>>8)&0xFF,ctldata,sdk_proto_get_tid(),sdk_context.mac);

    sdk_printf("%s:%s\r\n",__FUNCTION__,json);
    return sdk_proto_ctl_send((uint8_t *)json,strlen(json)+1);
}





